<?php

namespace app\admin\controller;

use think\Controller;
use think\Session;
use think\Request;
use think\Db;

class AdminController extends Controller
{
    //初始化的方法
    public function _initialize(){
        session_start();
        // var_dump($_SESSION);die;
        //判断session是否存在
        if(empty($_SESSION['think']['username'])){
            //跳转到 登陆页
            // var_dump(SESSION('id'));die;
            $this->redirect('/admin');
        }
        
        //权限过滤
        $mname = request()->controller(); //获取控制器名
        $aname = request()->action(); //获取方法名
        // var_dump($mname);
        // var_dump($aname);die;
        // echo $mname.'/'.$aname;
        //获取权限列表
        $nodelist = $_SESSION['think']['username']['nodelist'];
        // var_dump($nodelist[$mname]);
        // var_dump(in_array($aname,$nodelist[$mname]));die;
        // var_dump($nodelist[$mname]);die;
        // var_dump($_SESSION);die;
        // var_dump($_SESSION['username'][0]['username']);die;
        //让超级管理员admin拥有所有权限
        if($_SESSION['think']['username'][0]['username'] != 'admin'){
        //     //验证操作权限
            if(empty($nodelist[$mname]) || !in_array($aname,$nodelist[$mname])){
                // var_dump(666);
                $this->error("抱歉！没有操作权限！");
                exit;
            }

        }
    }
    /**
     * 后台主页
     * @return \think\response\View
     */
    public function index()
    {
        // 统计数据
        $tj = Db::name('a_count')->order('id','desc')->limit(7)->select();
        // dump($data);die;
        // 总注册用户
        $ap = Db('h_user')
              ->count('id');

        // 今天新增用户
        $beginToday = mktime(0,0,0,date('m'),date('d'),date('Y'));
        $endToday = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        $tp = Db('h_user')
                ->where('createtime','>',$beginToday)
                ->where('createtime','<',$endToday)
                ->count('id');
        // 总片源
         $am = Db('h_movie')
                ->count('id');
        // 今日评论数
        $tc = Db('h_comment')
                ->where('time','>',date('Y-m-s h:i:s',$beginToday))
                ->where('time','<',date('Y-m-s h:i:s',$endToday))
                ->count('id');
        // 计算总注册用户（昨日）
        
        $beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
        $endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;

        $yp = Db('h_user')
            ->where('createtime','>',$beginYesterday)
            ->where('createtime','<',$endYesterday)
            ->count('id');

        $aip = $ap - $yp;
        $aipl = number_format(($aip / $ap *100), 2);

        // 昨日新增用户
        $yip = Db('h_user')
                ->where('createtime','>',$beginYesterday)
                ->where('createtime','<',$endYesterday)
                ->count('id');
        $ip = $tp - $yip;
        if ($yip <= 0 || $ip <= 0) {
            $ipl = 0;
        }else{
            $ipl = number_format(($ip / $yip *100), 2);
        }

        // 昨日总片源
        $ym = Db('h_movie')
                ->where('myear','<',date('Y-m-s',$endYesterday))
                ->count('id');
        $im = $am - $ym;
        $aim = number_format(($im / $am *100), 2);

        // 昨日评论数
        $yc = Db('h_comment')
                ->where('time','>',date('Y-m-s h:i:s',$beginYesterday))
                ->where('time','<',date('Y-m-s h:i:s',$endYesterday))
                ->count('id');
        $ic = $tc - $yc;
        $aic = number_format(($ic / $tc *100), 2);

        //写入昨日数据到sm_a_count
            $time = date("Y-m-d",strtotime("-1 day"));
            $ctime = mktime(0,0,0,date('m'),date('d')-1,date('Y'));
            // dump($ctime);
            $s = [
            'time' => $time,
            'reg' => $ap,
            'mov' => $ym,
            'com' => $yc,
            'ctime' => $ctime,
        ];
        $t = Db::name('a_count')
                ->where('ctime', $ctime)
                ->select();
        if (empty($t)) {
            $data = Db::name('a_count')->insert($s);
        }
       
        return view('admin@AdminController/index', [
                'ap' => $aip,
                'tp' => $tp,
                'am' => $am,
                'tc' => $tc,
                'aipl' => $aipl,
                'ipl' => $ipl,
                'aim' => $aim,
                'aic' => $aic,
                'tj' => $tj,
            ]);
        
    }

}
